<?php
//class handset
// author : alfin akhret
class Handset extends CI_Controller
{
	public function __construct()
    {
            parent::__construct();
            $this->load->model('mmaster');
            $this->load->library('form_validation');
        	$this->load->library('enc');

    }

    function index()
    {
    	$this->handset();	
    }

    function handset($proses="",$id="") //tampilkan dataTables handset
    {
    	$data['template'] = "shell/smooth";
        $data['users']    = $this->mmaster->getHandset();
        $data['main_view'] = 'f-handset';
		$data['proses']	= $proses;
		$data['id']	= $id;
		
        $this->load->view($data['template'],$data);
    }

    function read() { //tampilkan dataTables handset
		$result = $this->mmaster->getHandset();

		//encode
        foreach ($result['aaData'] as $r => &$value) 
        {
            $value[0] = $this->enc->fogit($value[0]);
        }

		echo json_encode($result);
	}

	function update($id="")	//update data handset
	{
		$data['template'] = "shell/smooth";
        $data['users']    = $this->mmaster->getHandset();
        $data['main_view'] = 'f-handset';
		$data['proses']	= "update";
		// $data['id']	= $id;

		$id = $this->enc->defog($id); //decode the fog.
		$data['id']	= $id;

		$data['data'] = array("model"=>"","man"=>"","sales"=>"","imei"=>"", "imsi"=>"", "msisdn"=>"", "salesId"=>"");
		if($data['proses']<>"" && $id<>""){
		    $getdata = $this->mmaster->getHandset($id);
		   	//var_dump($getdata); echo "<br><br>";
		   	
		   	$data['data']['model'] 	= $getdata[0]->handset_model;
			$data['data']['man'] 	= $getdata[0]->handset_man;
			$data['data']['sales'] 	= $getdata[0]->user_name;
			$data['data']['salesId']= $getdata[0]->user_id;
			$data['data']['imei'] 	= $getdata[0]->imei;
			$data['data']['imsi'] 	= $getdata[0]->imsi;
			$data['data']['msisdn']	= $getdata[0]->msisdn;
		}

		#inisialisasi elemen form yang harus diisi
		$this->form_validation->set_rules('model', 'Model', 'required');
		$this->form_validation->set_rules('man', 'Man', 'required');
		$this->form_validation->set_rules('salesId', 'Sales', 'required');
		$this->form_validation->set_rules('imei', 'IMEI', 'required');
		$this->form_validation->set_rules('imsi', 'IMSI', 'required');
		$this->form_validation->set_rules('msisdn', 'MSISDN', 'required');

		$this->form_validation->set_error_delimiters('<li>','</li>');
		$this->form_validation->set_message('required', 'Kolom <b>%s</b> harus diisi.');
                
		if ($this->form_validation->run() != FALSE){
			// masukkan data di sini
			$this->mmaster->saveHandset($id);
			$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
			//redirect(base_url()."index.php/master/handset");
			redirect(base_url()."index.php/handset"); //diubah karena handset dibuatkan constructor baru
		}	
		
                $hasil = "";
                $this->load->view($data['template'],$data);

	}

	function delete( $id = null ) { //delete handset
		$id = $this->enc->defog($id); //decode the fog.
		
		if( is_null( $id ) ) {
			echo 'ERROR: ID tidak ditemukan';
			return;
		}
		$this->mmaster->deleteHandset( $id );
		echo 'Data berhasil dihapus';
	}

	function newHandset()
	{$data['template'] = "shell/smooth";
        $data['users']    = $this->mmaster->getHandset();
        $data['main_view'] = 'f-handset';
		$data['proses']	= "saveHandset";
		// $data['id']	= $id;

		$data['data'] = array("model"=>"","man"=>"","sales"=>"","imei"=>"", "imsi"=>"", "msisdn"=>"", "salesId"=>"");
		if($data['proses']<>""){
		    //$getdata = $this->mmaster->getHandset($id);
		   	//var_dump($getdata); echo "<br><br>";
		   	
		   	$data['data']['model'] 	= "";
			$data['data']['man'] 	= "";
			$data['data']['sales'] 	= "";
			$data['data']['salesId']= "";
			$data['data']['imei'] 	= "";
			$data['data']['imsi'] 	= "";
			$data['data']['msisdn']	= "";
		}

		#inisialisasi elemen form yang harus diisi
		$this->form_validation->set_rules('model', 'Model', 'required');
		$this->form_validation->set_rules('man', 'Man', 'required');
		$this->form_validation->set_rules('salesId', 'Sales', 'required');
		$this->form_validation->set_rules('imei', 'IMEI', 'required');
		$this->form_validation->set_rules('imsi', 'IMSI', 'required');
		$this->form_validation->set_rules('msisdn', 'MSISDN', 'required');

		$this->form_validation->set_error_delimiters('<li>','</li>');
		$this->form_validation->set_message('required', 'Kolom <b>%s</b> harus diisi.');
                
		if ($this->form_validation->run() != FALSE){
			// masukkan data di sini
			$this->mmaster->saveHandset($id);
			$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
			//redirect(base_url()."index.php/master/handset");
			redirect(base_url()."index.php/handset"); //diubah karena handset dibuatkan constructor baru
		}	
		
                $hasil = "";
                $this->load->view($data['template'],$data);
	}
}